home *** CD-ROM | disk | FTP | other *** search
/ The Original Shareware 1.1 / The Original Shareware (WeMake CDs)(Volume 1.1)(CDs, Inc)(1993).iso / 6 / c_wndw.zip / FILETEST.C < prev    next >
Text File  |  1989-05-25  |  3KB  |  75 lines

  1. /*  (c) Marietta Systems, Inc.  1987
  2. *    All rights reserved
  3. *
  4. *    Demonstration program for filexxxx functions
  5. */
  6. #include "mtest.h"
  7. void main(){
  8. long loc, rec_nbr = 0L;
  9. int fh1, fh2, z, x, k;
  10. clr_scrn("File access test");
  11. /*
  12. *    Create an ASCII file, write some records to it, and close it
  13. */ 
  14. fh1 = fileopen("testfile.dat", ascii, recreate);
  15. if (fh1 <= 0) goodbye(101);
  16. display("File opened - records being created", 1, 1, low);
  17. for (z = 1 , loc = 0L, x = 2; z < 21 ; z++, x++){
  18.      sprintf(FN[fh1].record, "This is record %.2u\n", z);
  19.      if (filewrit(fh1, &loc) < 0) goodbye(102); /* write error */
  20.      display(FN[fh1].record, x, 1, reverse);
  21.      }
  22. if (fileclos(fh1) < 1) goodbye(102);
  23. while (!disp_qry(" Ready to read the file"));
  24. /*
  25. *    Reopen the file, and read it until end of file, and close it
  26. */
  27. fh1 = fileopen("testfile.dat", ascii, readonly);
  28. if (fh1 <= 0) goodbye (103);
  29. display ("File opened - records read", 1, 40, low);   
  30. for (x = 2 ; ; x++){
  31.      if (!(k = fileread(fh1, nextrec, &rec_nbr))) break; /* EOF */
  32.      if (k < 0) goodbye(104); /* read error */
  33.      if (k) display(FN[fh1].record, x, 40, reverse);
  34.      }
  35. if (fileclos(fh1) < 1) goodbye(105);
  36. while (!disp_qry(" Ready to copy the file"));
  37. clr_wndw();
  38. /*
  39. *    reopen file 1, and copy it into a relative file with binary
  40. *    length records, stripping the '\n' symbol.
  41. */
  42. fh1 = fileopen("testfile.dat", ascii, readonly);
  43. fh2 = fileopen("testfile.rel", binary, recreate);
  44. if (fileinit(fh2, 0, 64, 0L)) goodbye(110);
  45. if (fh1 <= 0 || fh2 <= 0) goodbye (106);
  46. display ("File opened - records read", 1, 1, low);   
  47. for (x = 2 ; ; x++){
  48.      if (!(k = fileread(fh1, nextrec, &rec_nbr))) break; /* EOF */
  49.      if (k < 0) goodbye(107);             /* read error */
  50.      FN[fh1].record[strlen(FN[fh1].record) - 1] = 0; /* strip '\n' */
  51.      justify (left, FN[fh2].record, FN[fh1].record, 35, 0);
  52.      if (filewrit(fh2, &loc) < 0) goodbye(108); /* write error */
  53.      display(FN[fh2].record, x, 1, reverse);
  54.      }
  55. if (fileclos(fh1) < 1 || fileclos(fh2) < 1) goodbye(109);
  56. while (!disp_qry(" Ready to read file backwards"));
  57. /*
  58. *    reopen file 2, and read it backwards
  59. */
  60. fh2 = fileopen("testfile.rel", binary, readonly);
  61. if (fileinit(fh2, 0, 64, 20L)) goodbye(110);
  62. if (fh1 <= 0 || fh2 <= 0) goodbye (106);
  63. display ("File opened - records read", 1, 40, low);   
  64. k = fileread(fh2, lastrec, &rec_nbr);
  65. for (z = 20 , x = 2 ; z >= 0 ; z--){
  66.      if (k < 0) goodbye(107); /* read error */
  67.      if (!k) break;           /* EOF */
  68.      display(FN[fh2].record, x++, 40, reverse);
  69.      k = fileread(fh2, previous, &rec_nbr);
  70.      }
  71. if (fileclos(fh2) < 1) goodbye(109);
  72. disp_msg(" Press any key to end",1); read_kb();
  73. goodbye(0);
  74. }
  75.